/// @tags:
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>
#include <string>
#include <unordered_map>

using std::cin, std::cout, std::endl;

namespace BlueQuantum {

constexpr int maxn = 5e5 + 2;

struct Segment {
  int data, ls, rs;
} tree[24 * maxn];

int tot;
int root[maxn];

int build(int l, int r) {
  int p = ++tot;
  if (l == r) {
    tree[p].data = 0;
    return p;
  }
  int mid = (l + r) >> 1;
  tree[p].ls = build(l, mid);
  tree[p].rs = build(mid + 1, r);
  return p;
}

int modify(int cur, int pos, int l, int r, int val) {
  int p = ++tot;
  tree[p] = tree[cur];
  if (l == r) {
    tree[p].data = val;
    return p;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid) {
    tree[p].ls = modify(tree[cur].ls, pos, l, mid, val);
  } else {
    tree[p].rs = modify(tree[cur].rs, pos, mid + 1, r, val);
  }
  tree[p].data = tree[tree[p].ls].data || tree[tree[p].rs].data;
  return p;
}

int queryPos(int cur, int l, int r) {
  if (l == r) return l;
  int mid = (l + r) >> 1;
  if (tree[tree[cur].rs].data == 0)
    return queryPos(tree[cur].ls, l, mid);
  else
    return queryPos(tree[cur].rs, mid + 1, r);
}

int query(int cur, int pos, int l, int r) {
  if (l == r) return tree[cur].data;
  int mid = (l + r) >> 1;
  if (pos <= mid)
    return query(tree[cur].ls, pos, l, mid);
  else
    return query(tree[cur].rs, pos, mid + 1, r);
}

int main() {
  int q;
  cin >> q;
  std::unordered_map<int, int> map;
  root[0] = build(1, maxn - 2);
  int ro = root[0], cnt = 0;
  while (q--) {
    char opt[7];
    int x, y, z;
    cin >> opt;
    switch (opt[0]) {
      case 'A':
        cin >> x;
        if (tree[ro].data == 0) {
          root[++cnt] = modify(ro, 1, 1, maxn - 2, x);
        } else
          root[++cnt] =
              modify(ro, queryPos(ro, 1, maxn - 2) + 1, 1, maxn - 2, x);
        ro = root[cnt];
        break;

      case 'D':
        if (tree[ro].data != 0) {
          root[++cnt] = modify(ro, queryPos(ro, 1, maxn - 2), 1, maxn - 2, 0);
          ro = root[cnt];
        }
        break;
      case 'S':
        cin >> y;
        map[y] = ro;
        break;
      case 'L':
        cin >> z;
        ro = map[z];
        break;
    }
    int ans = query(ro, queryPos(ro, 1, maxn - 2), 1, maxn - 2);
    cout << (ans == 0 ? -1 : ans) << ' ';
  }

  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("E - Notebook.in", "r", stdin);
  freopen("E - Notebook.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
